# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=ogre3d
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=13.6.0
_imgui_ver=1.87
pkgrel=1
pkgdesc="A cross-platform 3D game engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.ogre3d.org/"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${MINGW_PACKAGE_PREFIX}-assimp"
             "${MINGW_PACKAGE_PREFIX}-freeimage"
            #  "${MINGW_PACKAGE_PREFIX}-openexr" https://github.com/OGRECave/ogre/issues/2179
             "${MINGW_PACKAGE_PREFIX}-pugixml"
             $([[ ${CARCH} != i686 ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-qt6-base" || echo \
               "${MINGW_PACKAGE_PREFIX}-qt5-base"))
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-bullet"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glsl-optimizer"
         "${MINGW_PACKAGE_PREFIX}-hlsl2glsl"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-tinyxml"
         "${MINGW_PACKAGE_PREFIX}-winpthreads"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-assimp"
            "${MINGW_PACKAGE_PREFIX}-freeimage"
            # "${MINGW_PACKAGE_PREFIX}-openexr"
            "${MINGW_PACKAGE_PREFIX}-python: python bindings"
            "${MINGW_PACKAGE_PREFIX}-pugixml: XML converter")
if [[ ${CARCH} != i686 ]]; then
  optdepends+=("${MINGW_PACKAGE_PREFIX}-qt6-base: integration with the Qt Library for window creation and input")
else
  optdepends+=("${MINGW_PACKAGE_PREFIX}-qt5-base: integration with the Qt Library for window creation and input")
fi
options=('!strip') # '!buildflags'
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/OGRECave/ogre/archive/v${pkgver}.tar.gz"
        https://github.com/ocornut/imgui/archive/v${_imgui_ver}.tar.gz
        002-link-shared-freeimage.patch
        004-use-mingw-w64-directx.patch
        010-missing-include.patch
        015-export-symbols-samples-plugin.patch
        016-fix-plugins-install-destination.patch)
sha256sums=('d32e58ad18d5fd9518f6a0dff4e566c281adc7a458b8231aa730983a8646bf95'
            'b54ceb35bda38766e36b87c25edf7a1cd8fd2cb8c485b245aedca6fb85645a20'
            '67966d6e0791c5804ed5cbec116594ebb72e9633118c31458dfa16181fdd01b2'
            'c7ca64dbd9a8f3f0271c12739807021a5c0782d254c4142c6e86746a96a44438'
            'd4e812fceb3dadc20c85857e3e624654cbc6bd4ebd20990fed2e5e84adfa9272'
            '004842e88cbbc76d09356fbddf5bc123b1b0fc7b2e64b8677b2a2316df7d9099'
            'ed6e383b1b4f1b84f1e93288540789c8fca0693a5dc9e3b4f840a63adbf5daac')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/ogre-${pkgver}"
  apply_patch_with_msg \
    002-link-shared-freeimage.patch \
    004-use-mingw-w64-directx.patch \
    010-missing-include.patch \
    015-export-symbols-samples-plugin.patch \
    016-fix-plugins-install-destination.patch
}

build() {
  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=( -DCMAKE_BUILD_TYPE=Debug )
  else
    extra_config+=( -DCMAKE_BUILD_TYPE=Release )
  fi

  CXXFLAGS+=" -Wno-ignored-attributes -Wno-inconsistent-missing-override"

  rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir build-${MSYSTEM} && cd build-${MSYSTEM}

  cp -r "${srcdir}"/imgui-${_imgui_ver} .

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DOGRE_DEPENDENCIES_DIR=${MINGW_PREFIX} \
    -DOGRE_INSTALL_TOOLS=ON \
    -DOGRE_COPY_DEPENDENCIES=OFF \
    -DOGRE_INSTALL_DEPENDENCIES=OFF \
    -DOGRE_BUILD_RENDERSYSTEM_D3D11=ON \
    -DOGRE_BUILD_RENDERSYSTEM_GL3PLUS=ON \
    -DOGRE_BUILD_COMPONENT_VOLUME=ON \
    -DOGRE_BUILD_COMPONENT_PAGING=ON \
    -DOGRE_BUILD_COMPONENT_TERRAIN=ON \
    -DOGRE_BUILD_COMPONENT_RTSHADERSYSTEM=ON \
    -DOGRE_BUILD_COMPONENT_PROPERTY=ON \
    -DOGRE_BUILD_COMPONENT_PYTHON=ON \
    -DOGRE_BUILD_COMPONENT_CSHARP=OFF \
    -DOGRE_CONFIG_DOUBLE=OFF \
    -DOGRE_BUILD_COMPONENT_JAVA=OFF \
    -DOGRE_BUILD_SAMPLES=ON \
    -DOGRE_INSTALL_SAMPLES=ON \
    -DOGRE_CONFIG_FILESYSTEM_UNICODE=ON \
    $([[ ${MSYSTEM_CARCH} == aarch64 ]] && echo "-DOGRE_BUILD_RENDERSYSTEM_D3D9=OFF") \
    -DOGRE_BUILD_TESTS=OFF \
    -DOGRE_INSTALL_PDB=OFF \
    -DOGRE_MEDIA_PATH=share/OGRE/Media \
    -DOGRE_CMAKE_DIR=lib/OGRE/cmake \
    -DOGRE_DOCS_PATH=share/doc/OGRE \
    -DOGRE_PLUGINS_PATH=lib/OGRE \
    -DOGRE_NODELESS_POSITIONING=ON \
    -DPYTHON_EXECUTABLE="${MINGW_PREFIX}/bin/python.exe" \
    "${extra_config[@]}" \
    ../ogre-${pkgver}

    ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/OGRE/cmake/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -Dm0644 ${srcdir}/ogre-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/OGRE/Media/fonts
}
